package org.opsli.modulars.business.realTimeTrail.web;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.opsli.api.base.result.ResultVo;
import org.opsli.api.wrapper.business.realTimeTrail.RealTimeTrailModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.persistence.Page;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.business.app.web.CoordinatesConvertUtil;
import org.opsli.modulars.business.app.web.MapCordinatesVo;
import org.opsli.modulars.business.realTimeTrail.entity.RealTimeTrail;
import org.opsli.modulars.business.realTimeTrail.service.RealTimeTrailService;
import org.opsli.modulars.business.realTimeTrail.service.impl.RealTimeTrailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@ApiRestController("/patrolRecord/realTimeTrail")
public class RealTimeTrailController extends BaseRestController<RealTimeTrail, RealTimeTrailModel, RealTimeTrailService> {

    @Autowired
    private RealTimeTrailServiceImpl rt;

    @RequestMapping(value = "/findUserRealTime", method = RequestMethod.POST)
    public ResultVo<?> getUser(Integer pageNo, Integer pageSize,String username) {
        String devid = UserUtil.getSysUser().getCompanyId();

        List<RealTimeTrail> list=new ArrayList<>();
        List<RealTimeTrail> trailList=rt.findUserName(devid,username);
        for (RealTimeTrail timeTrail :trailList){
            List<RealTimeTrail> list1=rt.findTask(devid,timeTrail.getUserId());
            if(null!=list1){
                for (RealTimeTrail realTimeTrail1:list1){
                    RealTimeTrail realTimeTrail =realTimeTrail1;
                    list.add(realTimeTrail);
                }
            }
        }

        list.sort(Comparator.comparing(RealTimeTrail::getCreTime).reversed());
        Page page = new Page(pageNo, pageSize);
        int totalLength = list.size(); //总的数据条数
        int totalPage = totalLength % pageSize == 0 ? totalLength / pageSize : totalLength / pageSize + 1; //总的页数
        List pageData = null;
        if (pageNo < totalPage) {
            int startIndex = (pageNo - 1) * pageSize;
            int endIndex = startIndex + pageSize - 1;
            pageData = list.subList(startIndex, endIndex + 1);
        } else {
            int startIndex = (pageNo - 1) * pageSize;
            pageData = list.subList(startIndex, totalLength);//最后一页
        }
        page.setList(pageData);
        page.setTotal(list.size());
        return ResultVo.success(page.getPageData());
    }

    @RequestMapping(value = "/findUserRealTimeTrial", method = RequestMethod.POST)
    public ResultVo<?> getTrail(String sign) {
        List list = new ArrayList();
        List l = rt.fingTrail(sign);
        String trail = null;
        String lastTime = null;
        if (l.size() > 0) {
            for (int i = 0; i < l.size(); i++) {
                RealTimeTrail r = (RealTimeTrail) l.get(i);
                trail = r.getTrail();
                lastTime=r.getLastTime();
            }
        }
        String time=null;
        if(null !=lastTime){
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = ft.parse(lastTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            SimpleDateFormat ft1 = new SimpleDateFormat("yyyy-MM-dd");
             time = ft1.format(date);
        }

        if (StringUtils.isNotBlank(trail)) {
            JSONArray jsonArray = JSONArray.parseArray(trail);
            for (int v = 0; v < jsonArray.size(); v++) {
                Map m = new HashMap();
                String lat = String.valueOf(jsonArray.getJSONObject(v).get("ln"));
                String lng = String.valueOf(jsonArray.getJSONObject(v).get("lt"));
                String tm = String.valueOf(jsonArray.getJSONObject(v).get("tm"));
                String lo = String.valueOf(jsonArray.getJSONObject(v).get("lo"));
                if (null ==tm || tm.equals("")) {
                    m.put("tm", "");
                }else {
                    String tms = time;
                    tms+=" ";
                    tms+=tm;
                    m.put("tm",tms);
                }
                MapCordinatesVo cordinatesVo = new MapCordinatesVo();
                cordinatesVo.setLon(new BigDecimal(lng));
                cordinatesVo.setLat(new BigDecimal(lat));
                cordinatesVo = CoordinatesConvertUtil.bd_encrypt(cordinatesVo);
                m.put("lt", cordinatesVo.getLon());
                m.put("ln", cordinatesVo.getLat());
                m.put("lo", lo);
                list.add(m);
            }
        }
        return ResultVo.success(list);
    }
}
